package com.stormrage.edu.dao.learn.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.learn.LeaTermDao;
import com.stormrage.entity.learn.LeaTerm;
import com.stormrage.entity.learn.rep.LeaTermRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: LeaTermDaoImpl.java
 * @Package com.stormrage.edu.dao.learn.impl
 * @Description: 学期 Dao
 * @author www.stormragetech.com
 * @date 2016年4月13日
 * @version V1.0
 */
@Repository
public class LeaTermDaoImpl extends MysBaseDao implements LeaTermDao {

	private static final Logger logger = LoggerFactory.getLogger(LeaTermDaoImpl.class);

	@Override
	public Integer create(final LeaTerm arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 Create 】- 开始运行 ");

		final String query = "INSERT INTO lea_term (term, major_id, bunch, apply_date_start, apply_date_end,"
				+ " study_date_start, study_date_end, exam_date_start, exam_date_end) VALUES (:term, :majorId, :bunch,"
				+ " :applyDateStart, :applyDateEnd,:studyDateStart, :studyDateEnd,:examDateStart, :examDateEnd)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public void update(final LeaTerm arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 Update 】- 开始运行 ");

		final String query = "UPDATE lea_term SET term = :term, major_id = :majorId, bunch = :bunch, apply_date_start = :applyDateStart,"
				+ " apply_date_end = :applyDateEnd, study_date_start =:studyDateStart, study_date_end = :studyDateEnd,"
				+ " exam_date_start = :examDateStart, exam_date_end = :examDateEnd WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 Update 】- 完成运行 ");

	}

	@Override
	public void delete(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 Delete 】- 开始运行 ");

		final String query = "DELETE FROM lea_term WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 Delete 】- 完成运行 ");

	}

	@Override
	public Integer queryCount(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryCount 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM lea_term " + map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryCount 】- 完成运行 ");
		return count;
	}

	@Override
	public Integer queryRepCount(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryCount 】- 开始运行 ");

		final String query = "SELECT COUNT(a.id) FROM lea_term AS a, edu_major AS b WHERE a.major_id = c.id "
				+ map.get("query") + " ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryCount 】- 完成运行 ");
		return count;
	}

	@Override
	public LeaTerm queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM lea_term WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		LeaTerm entity = queryObject(query, LeaTerm.class, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryById 】- 完成运行 ");
		return entity;

	}

	@Override
	public LeaTerm queryByMajorIdDate(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryByMajorIdDate 】- 开始运行 ");

		final String query = "SELECT * FROM lea_term WHERE major_id = :majorId AND now() < exam_date_end";
		logger.debug(" 数据库执行语句为 " + query);

		LeaTerm entity = queryObject(query, LeaTerm.class, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryByMajorIdDate 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<LeaTerm> queryList() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM lea_term";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaTerm> array = queryList(query, LeaTerm.class);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryList 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaTerm> queryList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM lea_term" + map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaTerm> array = queryList(query, LeaTerm.class, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryList 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaTermRep> queryRepList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT a.id, a.term, a.apply_date_start, a.study_date_start, a.exam_date_start, b.major,"
				+ " (SELECT COUNT(*) FROM lea_bunch AS c WHERE c.term_id = a.id ) AS count"
				+ " FROM lea_term AS a, edu_major AS b WHERE a.major_id = b.id " + map.get("query")
				+ " ORDER BY a.id LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaTermRep> array = queryList(query, LeaTermRep.class, map);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 QueryList 】- 完成运行 ");
		return array;

	}

	@Override
	public List<LeaTermRep> queryRepListForExam() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 queryRepListForExam 】-【 STARTED 】");

		final String query = "SELECT a.id, a.term, a.exam_date_start, a.exam_date_end, b.major, b.module, c.sele,"
				+ " c.mult, c.corr, (SELECT COUNT(*) FROM lea_bunch AS c WHERE c.term_id = a.id ) AS count "
				+ "FROM lea_term AS a, edu_major AS b, exm_term_score AS c WHERE a.major_id = b.id"
				+ " AND NOW() < a.exam_date_end AND a.id = c.term_id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<LeaTermRep> array = queryList(query, LeaTermRep.class);

		logger.debug(" 【 Dao 】- 【 LeaTermDao 】- 【 queryRepListForExam 】-【 COMPLETED 】");
		return array;

	}

}
